// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import iview from 'iview'
import base from '@/service/Base.vue'

Vue.use(iview)

Vue.config.productionTip = true

/* eslint-disable no-new */

var axiosConfig = {
  // baseURL: 'http://rap.taobao.org/mockjsdata/5214',
  baseURL: 'http://10.10.1.219:9906',
  // baseURL: 'http://localhost:8080',
  // url: 'api/hedacmdreq',
  url: '',
  responseType: 'text',
  timeout: 4000,
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  // transformResponse: [function (data) {  // 相应拦截器
  //   // console.log('响应数据：', data)
  //   // if (data) {
  //   //   return false
  //   // }
  //   return JSON.parse(data)
  // }]
}
var instance = axios.create(axiosConfig)

Vue.prototype.$http = instance
// http request 拦截器
instance.interceptors.request.use(
  config => {
    // if (store.state.token) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
    //     config.headers.Authorization = `token ${store.state.token}`;
    // }
    window.duang.loading()
    return config
  },
  err => {
    window.duang.closeLoading()
    return Promise.reject(err)
  }
)
instance.interceptors.response.use(
  response => {
    window.duang.closeLoading()
    if (response.data.HedaACK && response.data.HedaACK.Body.retCode === '1') {
      console.warn('异常：' + response.data.HedaACK.Body.retMsg)
      window.duang.msg(false, response.data.HedaACK.Body.retMsg)
    } else if (!response || !response.data.HedaACK) {
      console.error('请求异常', response)
      response.data = {
        HedaACK: {
          Body: {
            retCode: '-1',
            retMsg: '系统异常，请稍后再试'
          }
        }
      }
    }
    console.log('请求响应数据', response.config.data, response)
    return response
  },
  function (error) {
    window.duang.closeLoading()
    if (error.toString().indexOf('timeout') >= 0) {
      window.duang.msg(false, '服务器连接超时，请网络或服务器')
    }
    return Promise.reject(error)   // 返回接口返回的错误信息
  }
)
Vue.use(instance)
// Vue.prototype.$http = instance

router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !base.user) {
    router.push({name: 'Login'})
  }
  // if (to.name !== 'Login' && !base.user && !window.duang.getData('user')) {
  //   router.push({name: 'Login'})
  // } else if (window.duang.getData('user')) {
  //   // Login.data().pwd = '123456'
  //   // console.log(Login.computed.md5Pwd())
  //   // var user = window.duang.getData('user')
  //   // Login.userid = user.userid
  //   // Login.pwd = user.pwd
  //   // Login.methods.doLogin()
  // }
  next()
})

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
